package com.qf.fcy.youshare.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.qf.fcy.youshare.Constants;
import com.qf.fcy.youshare.R;
import com.qf.fcy.youshare.model.Product;
import com.qf.fcy.youshare.utils.FileCache;
import com.qf.fcy.youshare.utils.ImageCache;
import com.qf.fcy.youshare.utils.ImageLoader;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Alex[FR]
 * Date: 2015/2/6
 * Email: alex_fcy@163.com
 */

/**
 * 生成核心产品列表的Adapter
 */
public class StoreCoreAdapter extends BaseAdapter {
    // 用于设置ListView条目的背景
    private static int[] colors = new int[]{0xFFFA950A, 0xFF157FC9, 0xFF70B10F,
            0xFF907C5F, 0xFFD42749, 0xFF6A588E};

    private List<Product> products;
    private LayoutInflater inflater;

    public StoreCoreAdapter(Context context, List<Product> products) {
        if (context != null) {
            inflater = LayoutInflater.from(context);
        } else {
            throw new IllegalStateException("Context must not be null");
        }
        this.products = products;
    }

    @Override
    public int getCount() {
        return products.size();
    }

    @Override
    public Object getItem(int position) {
        return products.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_store_core, parent, false);
            holder.txtPosition = (TextView) convertView.findViewById(R.id.item_core_position);
            holder.txtShortTitle = (TextView) convertView.findViewById(R.id.item_core_short_title);
            holder.txtTitle = (TextView) convertView.findViewById(R.id.item_core_title);
            holder.imgIcon = (ImageView) convertView.findViewById(R.id.item_core_img);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Product product = products.get(position);

        int loc = position + 1;
        String s = Integer.toString(loc);
        if (loc < 10) {
            s = "0" + s;
        }
        holder.txtPosition.setText(s);

        // 可能为空的，通过optXXX解析的，需要判断inn?好像是字符串为""
        String appGname = product.getAppGname();
        if (appGname != null) {
            holder.txtShortTitle.setText(appGname);
        }

        holder.txtTitle.setText(product.getGoodsName());
        // 设置默认图片防止图片复用显示之前的图片
        // 应该先判断图片缓存是否存在，如果存在直接显示
        // 不存在，则先显示默认图片，再开异步任务加载
        convertView.setBackgroundColor(colors[position % colors.length]);
        String goodsThumb = product.getGoodsThumb();
        // 拼接图片网址
        String reqUrl = Constants.IMG_SERVER + "/" + goodsThumb;
        //TODO 加载图片
        if (goodsThumb != null && goodsThumb.length() > 0) {
            if (!goodsThumb.startsWith("http://") && !goodsThumb.startsWith("https://")) {//有效网址
                holder.imgIcon.setTag(reqUrl);

                // 进行内存缓存的检查，如果有图片，直接加载
                ImageCache imageCache = ImageCache.getInstance();
                Bitmap bitmap = imageCache.getImage(reqUrl);
                if (bitmap != null) {// 如果内存中包含图片对象 Bitmap 那么就直接设置 ImageView
                    holder.imgIcon.setImageBitmap(bitmap);
                } else { // 不包含，则检查本地文件是否存在缓存
                    //TODO 检查本地文件是否存在图片缓存
                    FileCache fileCache = FileCache.getInstance();
                    byte[] bytes = fileCache.getContent(reqUrl);
                    if (bytes != null && bytes.length > 0) {
                        Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                        holder.imgIcon.setImageBitmap(bmp);

                        imageCache.putImage(reqUrl, bmp);
                    } else {
                        // TODO 本地不存在图片缓存，则先设置默认图片，再开启异步任务下载图片
                        holder.imgIcon.setImageResource(R.drawable.product_default);
                        ImageLoader task = new ImageLoader(holder.imgIcon);
                        task.execute(reqUrl);
                    }
                }
            } else {
                Log.d("YouShare", "服务器图片网址错误");
            }
        }

        return convertView;
    }

    private static class ViewHolder {
        public TextView txtPosition;
        public TextView txtShortTitle;
        public TextView txtTitle;
        public ImageView imgIcon;
    }

}
